import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class DAY0315 {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer,Integer> map=new HashMap<Integer, Integer>();
        for (int i = 0; i < nums.length; i++) {
            if(map.containsKey(target-nums[i])){
                return new int[]{map.get(target-nums[i]),i};
            }
            map.put(nums[i],i);
        }
        return new int[0];
    }

    public void merge(int[] nums1, int m, int[] nums2, int n) {
        for(int i=0;i!=n;++i){
            nums1[m+i]=nums2[i];
        }
        Arrays.sort(nums1);
    }

    public int searchInsert(int[] nums, int target) {
        int n=nums.length;
        int left=0,right=n-1,num=n;
        while(right>=left){
            int mid=(right-left)/2+left;
            if(nums[mid]>=target){
                num=mid;
                right=mid-1;
            }else{
                left=mid+1;
            }
        }
        return num;
    }



    public int peakIndexInMountainArray(int[] arr) {
        int left=1;
        int right=arr.length-2;
        int ans=0;
        while (left<=right){
            int mid=(left+right)/2;
            if(arr[mid]>arr[mid+1]){
                ans=mid;
                right=mid-1;
            }else {
                left=mid+1;
            }
        }
        return ans;
    }
}
